package fun.ticsmyc.question.dp;

/**
 * 10. 正则表达式匹配
 * @author Ticsmyc
 * @package fun.ticsmyc.question.stringQuestion
 * @date 2021-02-05 12:22
 */
public class 正则表达式匹配 {

    public boolean isMatch(String s, String p) {
        char[] str = s.toCharArray();
        char[] ptr = p.toCharArray();
        int m = ptr.length;
        int n =str.length;
        boolean[][] dp = new boolean[m+1][n+1];
        dp[0][0]=true;
        for(int i=1;i<=n;++i){
            dp[0][i]=false;
        }
        for(int i=1;i<=m;++i){
            for(int j=0;j<n;++j){
                if(ptr[i-1]=='.'){
                    dp[i][j] = dp[i-1][j-1];
                }else if(ptr[i-1]=='*'){
                    dp[i][j] = dp[i-2][j] || dp[i-1][j] ||( (str[j-1] == ptr[i-2] ||ptr[i-2]=='.') ?dp[i][j-1]:false);
                }else{
                    dp[i][j] = (str[j-1] ==ptr[i-1])?dp[i-1][j-1] : false;
                }
            }
        }
        return dp[m][n];
    }
}
